{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f223a430-4e0d-4600-bef4-f3d8eff88b0f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   fixed acidity  volatile acidity  citric acid  residual sugar  chlorides  \\\n",
      "0            7.0              0.27         0.36            20.7      0.045   \n",
      "1            8.1              0.28         0.40             6.9      0.050   \n",
      "2            7.2              0.23         0.32             8.5      0.058   \n",
      "3            7.2              0.23         0.32             8.5      0.058   \n",
      "4            8.1              0.28         0.40             6.9      0.050   \n",
      "\n",
      "   free sulfur dioxide  total sulfur dioxide  density    pH  sulphates  \\\n",
      "0                 45.0                 170.0   1.0010  3.00       0.45   \n",
      "1                 30.0                  97.0   0.9951  3.26       0.44   \n",
      "2                 47.0                 186.0   0.9956  3.19       0.40   \n",
      "3                 47.0                 186.0   0.9956  3.19       0.40   \n",
      "4                 30.0                  97.0   0.9951  3.26       0.44   \n",
      "\n",
      "   alcohol  quality  \n",
      "0      8.8        6  \n",
      "1     10.1        6  \n",
      "2      9.9        6  \n",
      "3      9.9        6  \n",
      "4     10.1        6  \n"
     ]
    }
   ],
   "source": [
    "# 读取数据（写出代码），并打印前5行。\n",
    "import pandas as pd\n",
    "try:\n",
    "    file_path = \"D:/python_w/python_w/white_wine.csv\"\n",
    "    df = pd.read_csv(file_path, encoding=\"gbk\")\n",
    "except Exception as e:\n",
    "    print(e)\n",
    "else:\n",
    "    print(df.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "c1e08ac0-41eb-4976-bf2f-31d62a65e000",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[6 5 7 8 4 3 9]\n"
     ]
    }
   ],
   "source": [
    "# 查看白葡萄酒总共分为几种品质等级\n",
    "quality = df['quality'].unique()\n",
    "print(quality)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "1e125bf8-aabf-4669-a613-98bafb82e39c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "品质等级 6 的样本量为: 1539\n",
      "品质等级 5 的样本量为: 1020\n",
      "品质等级 7 的样本量为: 616\n",
      "品质等级 8 的样本量为: 123\n",
      "品质等级 4 的样本量为: 115\n",
      "品质等级 3 的样本量为: 14\n",
      "品质等级 9 的样本量为: 4\n"
     ]
    }
   ],
   "source": [
    "# 按白葡萄酒等级将数据集划分为7个子集,统计在每个品质的样本量\n",
    "sample_size = {}\n",
    "for i in quality:\n",
    "    subset = df[df['quality'] == i]\n",
    "    sample_size = len(subset)\n",
    "    print(f\"品质等级 {i} 的样本量为: {sample_size}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "97c0cc5c-1dda-4c2d-8ba9-9a3121d89060",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "品质等级 3 的样本量为: 14\n",
      "品质等级 4 的样本量为: 115\n",
      "品质等级 5 的样本量为: 1020\n",
      "品质等级 6 的样本量为: 1539\n",
      "品质等级 7 的样本量为: 616\n",
      "品质等级 8 的样本量为: 123\n",
      "品质等级 9 的样本量为: 4\n"
     ]
    }
   ],
   "source": [
    "quality_counts = df['quality'].value_counts().sort_index()\n",
    "for quality, count in quality_counts.items():\n",
    "    print(f'品质等级 {quality} 的样本量为: {count}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "f47af235-ab1b-407a-a761-b76911f2847a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "品质等级 6 的的均值为: 6.8120857699805075\n",
      "品质等级 5 的的均值为: 6.907843137254902\n",
      "品质等级 7 的的均值为: 6.755844155844157\n",
      "品质等级 8 的的均值为: 6.708130081300812\n",
      "品质等级 4 的的均值为: 7.052173913043478\n",
      "品质等级 3 的的均值为: 7.535714285714286\n",
      "品质等级 9 的的均值为: 7.5\n"
     ]
    }
   ],
   "source": [
    "# 求每个数据集中fixed acidity的均值\n",
    "quality = df['quality'].unique()\n",
    "mean = {}\n",
    "for j in quality:\n",
    "    subset = df[df['quality'] == j]\n",
    "    sz = len(subset)\n",
    "    sum_fixed = subset['fixed acidity'].sum()\n",
    "    mean = sum_fixed / sz\n",
    "    print(f\"品质等级 {j} 的的均值为: {mean}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3cb804bf-59f4-49ae-beed-2b231741d66b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
